package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.common.BaseContextUtils;
import com.itheima.common.R;
import com.itheima.domain.AddressBook;
import com.itheima.domain.OrderDetail;
import com.itheima.domain.Orders;
import com.itheima.domain.User;
import com.itheima.dto.OrdersDto;
import com.itheima.service.AddressBookService;
import com.itheima.service.OrderDetailService;
import com.itheima.service.OrdersService;
import com.itheima.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private UserService userService;

    /**
     * 支付功能
     *
     * @param orders
     * @return
     */
    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders) {
        ordersService.submit(orders);
        return R.success("下单成功");
    }

    /**
     * 订单查询
     * @param page
     * @param pageSize
     * @return
     */
    @GetMapping("/userPage")
    @Transactional
    public R<Page<OrdersDto>> userPage(Integer page, Integer pageSize) {
        Page<OrdersDto> ipage = new Page<>(page, pageSize);

        //查询 订单表
        Page<Orders> ordersPageage = new Page<>(page, pageSize);
        ordersService.page(ordersPageage);
        List<Orders> orderList = ordersPageage.getRecords();
        //查询地址表
        LambdaQueryWrapper<AddressBook> lqw = new LambdaQueryWrapper<>();
        lqw.eq(AddressBook::getUserId, BaseContextUtils.getId());
        AddressBook address = addressBookService.getOne(lqw);

        List<OrdersDto> list = orderList.stream().map((item) -> {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(item, ordersDto, "records");
            //  设置订单属性
            ordersDto.setId(item.getId());
            ordersDto.setNumber(item.getNumber());
            ordersDto.setStatus(item.getStatus());
            ordersDto.setUserId(item.getUserId());
            ordersDto.setAddressBookId(item.getAddressBookId());
            ordersDto.setOrderTime(item.getOrderTime());
            ordersDto.setCheckoutTime(item.getCheckoutTime());
            ordersDto.setPayMethod(item.getPayMethod());
            ordersDto.setAmount(item.getAmount());
            ordersDto.setUserName(item.getUserName());
            ordersDto.setPhone(item.getPhone());
            ordersDto.setAddress(item.getAddress());
            ordersDto.setConsignee(item.getConsignee());
            //查询 订单详情表
            Long id = item.getId();
            LambdaQueryWrapper<OrderDetail> lqw2 = new LambdaQueryWrapper<>();
            lqw2.eq(OrderDetail::getOrderId, id);
            List<OrderDetail> list1 = orderDetailService.list(lqw2);
            log.info("list1=={}",list1);
            ordersDto.setOrderDetails(list1);
            //  设置 Dto 其他属性
            ordersDto.setUserName("林之迷");
            ordersDto.setPhone(address.getPhone());
            ordersDto.setAddress(address.getDetail());
            ordersDto.setConsignee(address.getConsignee());
            return ordersDto;
        }).collect(Collectors.toList());
        ipage.setRecords(list);

        log.info("list=={}", list);
        return R.success(ipage);
    }

    /**
     * 管理员 分页查询
     * @param page
     * @param pageSize
     * @param number
     * @return
     */
    @GetMapping("/page")
    public R<Page<Orders>> page(Integer page, Integer pageSize,String number){
        Page<Orders> ipage =new Page<>(page,pageSize);
        LambdaQueryWrapper<Orders> lqw =new LambdaQueryWrapper<>();
        lqw.like(number!=null,Orders::getNumber,number);
        ordersService.page(ipage,lqw);
        return R.success(ipage);
    }

    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        ordersService.updateById(orders);
        return R.success("修改成功");

    }
}